'use strict'

yjx.controller('SendCodeController', ['$rootScope', '$scope', 'ipCookie', '$timeout', '$interval', '$fetch', 'LoginService', 'modalService',
    function SendCodeController($rootScope, $scope, ipCookie, $timeout, $interval, $fetch, LoginService, modalService) {
        //console.info("RegisterController");
        var vm = this;

        //初始化变量 默认为提现获取验证码
        var APIsendCode = '/validCode/send';

        var sendText = '获取验证码';
        $scope.sendText = sendText;
        $scope.sendStatus = 'block';

        /**获取验证码*/
        vm.sendCode = function (phone, type) {
            if (!phone || !phone.match("^[1][34578][0-9]{9}$")) {
                modalService.mainMsgError("请输入正确格式的手机号");
                return;
            }
            $timeout(function () {
                $scope.tagStatus = true;
                $scope.sendText = '发送中..';
            });
            var params = {}
            if (type == 'register') {
                if ($rootScope.identityTo == 1) {
                    APIsendCode = '/register/manager/send'
                } else {
                    APIsendCode = '/register/customer/send'
                }
                params.telephone = phone
            } else {
                params.userId = $rootScope.userId
            }

            $fetch.fetch(
                APIsendCode, //
                {
                    params: params,
                    cache: false,
                    type: 'post',
                }
            ).then(function (data) {
                //验证码已发送注意查收
                //定时器timer
                var timer = 120;
                var time = timer;
                $timeout(function () {
                    //重置定时器对象
                    typeof ($scope.tagTimer) == 'undefined' ? 'none' : $interval.cancel($scope.tagTimer);
                    $scope.sendStatus = 'sending';//发送页面
                    $scope.tagStatus = true;//按钮状态 改变为disable
                    if (phone) {
                        $scope.phone = phone;
                    }
                    $scope.sendText = '获取(' + time + ')';
                });
                $timeout(function () {
                    //定义定时器对象
                    $scope.tagTimer = $interval(function () {
                        time--;
                        if (time > 0) {
                            $scope.sendText = '获取(' + time + ')';
                        } else {
                            //time=timer;
                            $scope.tagStatus = false;
                            $scope.sendText = sendText;
                            return;
                        }
                        //console.info(time);
                    }, 1000, time);
                });
            }).catch(function (e) {
                $timeout(function () {
                    typeof ($scope.tagTimer) == 'undefined' ? 'none' : $interval.cancel($scope.tagTimer);
                    modalService.mainMsgError(e.msg);
                    $scope.tagStatus = false;
                    $scope.sendText = sendText;
                    return;
                });
            });
        }

    }]);